﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Text;

public partial class _Default : webapp4U.UI.BasePage
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TrackingLogOut();
            Session.Abandon();
            GetRandomText();
        }
    }
    void TrackingLogOut()
    {
        if (Session["user"] != null)
        {
            dt = new ClassExecute.ClassExecute();
            dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "k_TrackingLogOut", ref error,
                new SqlParameter("@Username", Session["user"].ToString()));
        }
    }
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }  
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        ClickLogin();
    }
    private void GetRandomText()
    {

        StringBuilder randomText = new StringBuilder();

      
            string alphabets = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
            Random r = new Random();

            for (int j = 0; j <= 3; j++)
            {
                randomText.Append(alphabets[r.Next(alphabets.Length)]);
            }
            
       
        lblCatCha.Text= randomText.ToString();
    }
    void ClickLogin()
    {
        dt = new ClassExecute.ClassExecute();
        string kq = "notauthentication";
        try
        {
            if (txtCatcha.Text.Trim() == lblCatCha.Text)
            {
                kq = dt.GetDataDepend(str, "k_checkLogin", CommandType.StoredProcedure, ref error,
                   new SqlParameter("@UserName", txtUser.Text),
                   new SqlParameter("@Password", dt.GetMd5Sum(txtPass.Text)));

                if ((kq != "notauthentication") && (kq != ""))
                {
                    Session["user"] = txtUser.Text;
                    Session["UserType"] = kq;
                    //Session["sitecode"] = dt.GetDataDepend(str, "k_getSiteCodeByUser", CommandType.StoredProcedure, ref error,
                        //new SqlParameter("@UserName", txtUser.Text));
                   
                    Response.Redirect(webapp4U.Globals.ApplicationPath + "Pages/Management/Default.aspx", true);
                }
                else
                {
                    ShowMessageBox(Resources.LanguageResource.InvalidLogin);
                    txtCatcha.Text = "";
                    GetRandomText();
                }
            }
            else
            {
                ShowMessageBox(Resources.LanguageResource.InvalidCode);
                txtCatcha.Text = "";
                GetRandomText();
            }
        }
        catch 
        {
            return;
        }
    }
    
}